package com.ring.server;

import com.ring.server.event.MessageQoSEventListenerS2C;
import com.ring.server.event.ServerEventListener;
import com.ring.server.network.Gateway;
import com.ring.server.network.GatewayTCP;
import com.ring.server.network.GatewayUDP;
import com.ring.server.qos.QoS4ReciveDaemonC2S;
import com.ring.server.qos.QoS4SendDaemonS2C;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @since 2023-10-09 19:33
 */
public class ServerLauncher {

    private static Logger logger = LoggerFactory.getLogger(ServerLauncher.class);

    public static boolean serverTimestamp = false;

    public static boolean bridgeEnabled = false;

    public static int supportedGateways = 0;

    protected ServerCoreHandler serverCoreHandler = null;

    private boolean running = false;

    private Gateway udp = null;

    private Gateway tcp = null;

    private Gateway ws = null;

    private final ServerConfig serverConfig;

    public ServerLauncher(ServerConfig serverConfig) {
        this.serverConfig = serverConfig;
        this.serverCoreHandler = new ServerCoreHandler();
    }

    protected void initGateways() {
        if (Gateway.isSupportUDP(supportedGateways)) {
            udp = new GatewayUDP();;
            udp.init(this.serverCoreHandler);
        }

        if (Gateway.isSupportTCP(supportedGateways)) {
            tcp = createGatewayTCP();
            tcp.init(this.serverCoreHandler);
        }

        /*if (Gateway.isSupportWebSocket(supportedGateways)) {
            ws = createGatewayWebsocket();
            ws.init(this.serverCoreHandler);
        }*/
    }


    protected GatewayTCP createGatewayTCP() {
        return new GatewayTCP();
    }

    /*protected GatewayWebsocket createGatewayWebsocket() {
        return new GatewayWebsocket();
    }
*/
    public void startup() throws Exception {
        if (!this.running) {
            initGateways();
            QoS4ReciveDaemonC2S.getInstance().startup();
            QoS4SendDaemonS2C.getInstance().startup(true).setServerLauncher(this);

            if (ServerLauncher.bridgeEnabled) {
                serverCoreHandler.lazyStartupBridgeProcessor();
                logger.info("[IMCORE] 配置项：已开启与MobileIMSDK Web的互通.");
            } else {
                logger.info("[IMCORE] 配置项：未开启与MobileIMSDK Web的互通.");
            }

            bind();
            this.running = true;
        } else {
            logger.warn("[IMCORE] 基于MobileIMSDK的通信服务正在运行中，本次startup()失败，请先调用shutdown()后再试！");
        }
    }

    protected void bind() throws Exception {
        if (udp != null) {
            udp.bind();
        }
        if (tcp != null) {
            tcp.bind();
        }
        if (ws != null) {
            ws.bind();
        }
    }

    public void shutdown() {
        if (udp != null) {
            udp.shutdown();
        }
        if (tcp != null) {
            tcp.shutdown();
        }
        if (ws != null) {
            ws.shutdown();
        }

        QoS4ReciveDaemonC2S.getInstance().stop();
        QoS4SendDaemonS2C.getInstance().stop();

        this.running = false;
    }

    public ServerEventListener getServerEventListener() {
        return serverCoreHandler.getServerEventListener();
    }

    public void setServerEventListener(ServerEventListener serverEventListener) {
        this.serverCoreHandler.setServerEventListener(serverEventListener);
    }

    public MessageQoSEventListenerS2C getServerMessageQoSEventListener() {
        return serverCoreHandler.getServerMessageQoSEventListener();
    }

    public void setServerMessageQoSEventListener(MessageQoSEventListenerS2C serverMessageQoSEventListener) {
        this.serverCoreHandler.setServerMessageQoSEventListener(serverMessageQoSEventListener);
    }

    public boolean isRunning() {
        return running;
    }
}
